package sphere.controller;

import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import sphere.model.Sphere;
import sphere.view.SphereWindow;

public class SphereController implements ActionListener{

	private SphereWindow sphereWindow;
	private Sphere sphere;
	public SphereController(SphereWindow sphereWindow) {
		this.sphereWindow = sphereWindow;
		this.sphere = new Sphere(0);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		try{
			String text = ((TextField) e.getSource()).getText();
			double rayon = Double.parseDouble(text);
			this.sphere.setRayon(rayon);
			this.sphereWindow.getTextFieldSurface().setText(String.valueOf(this.sphere.surface()));
			this.sphereWindow.getTextFieldVolume().setText(String.valueOf(this.sphere.volume()));
		} catch(NumberFormatException ex){
			this.sphereWindow.getTextFieldSurface().setText("");
			this.sphereWindow.getTextFieldVolume().setText("");
		}
	}

}
